//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
/*
  Homework 3 CSU213 Proulx
  Adam Blackwell
  8333
  James Morin
  9759?
*/

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//Problem 15.8 on page 175
//Design the following methods for the class hierarchy representing
//river systems.
//1. maxLength, which computes the length of the longest path
//through the river system;
//2. confluences, which counts the number of confluences in the
//river system; and
//3. locations, which produces a list of all locations on this river,
//including sources, mouths, and confluences.

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//Problem 15.11 on page 176
//Figure 59 contains a class diagram that descrives the GUI hierarchy
//of exercise 6.5. Note that one of its interfaces, ITable, refines
//another interface IGUIComponent.
//Add templates to each box in this diagram. Do not assume anything
//about the return type; do not design classes.

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//Exercise 3
//This problem continues the work on mobiles we have started during
//the lectures. The file mobile-methods-lecture.java contains the
//data definitions, examples of data, and the method totalWeight
//we have designed in class.
//Design the method draw() that consumes a Canvas and a Posn
//that represents the point where the top of the mobile will hang. The
//method draws the mobile with black lines for the struts, and for the
//hanging lines. For a simple mobile, there should be a disk of the 
//appropriate color and with the size proportionate to its weight shown
//at the end of the line.

// CS U213 Spring 2009
// Assignment 3
// Portfolio Problem 3
// October 2, 2008
// mobile-methods.java
/*
  Sample mobiles:
  ---------------
  
  Simple mobile      |
                     |
                     10
                    blue
  
  Complex mobile         |
                         |
                         |
             ------------+-----
             |                |
       ------+------          |
       |           |          | 
       10          |          40    
      red          10        green
                  blue
*/
import draw.*;
import geometry.*;
import colors.*;
import java.util.*;
/*
               +---------+                  
               | IMobile |<---------------+
               +---------+                |
               +---------+                |
                   |                      |
                  / \                     |
                  ---                     |
                   |                      |
         ---------------------            |
         |                   |            |
  +--------------+   +---------------+    |
  | Simple       |   | Complex       |    |
  +--------------+   +---------------+    |
  | int length   |   | int length    |    |
  | int weight   |   | int leftside  |    |
  | IColor color |   | int rightside |    |
  +--------------+   | IMobile left  |----+ 
                     | IMobile right |----+  
                     +---------------+ 
*/

// to represent a mobile
interface IMobile {

  // compute the total weight of this mobile
  int totalWeight();
  
  // compute the total length of this mobile
  //int totalLength();
  
  // is this mobile is balanced?
  //boolean isBalanced();

}

// to represent an item hanging at the end of a mobile
class Simple implements IMobile {
  int length;
  int weight;
  IColor color;

  Simple(int length, int weight, IColor color) {
    this.length = length;
    this.weight = weight;
    this.color = color;
  }

/* TEMPLATE:
   FIELDS:
    ... this.length ...         -- int
    ... this.weight ...         -- int
    ... this.color ...          -- IColor

    METHODS:
    ... this.totalWeight() ...      -- int
    ... this.totalLength() ...      -- int
    ... this.isBalanced() ...        -- boolean
*/

  // compute the total weight of this simple mobile
  int totalWeight(){
    return this.weight;
  }
  
}

// to represent a part of support structure for a mobile
class Complex implements IMobile {
  int length;
  int leftSide;
  int rightSide;
  IMobile left;
  IMobile right;

  Complex(int length, int leftSide, int rightSide, 
          IMobile left, IMobile right) {
    this.length = length;
    this.leftSide = leftSide;
    this.rightSide = rightSide;
    this.left = left;
    this.right = right;
  }

/* TEMPLATE:
   FIELDS:
    ... this.length ...           -- int
    ... this.leftside ...         -- int
    ... this.rightside ...        -- int
    ... this.left ...             -- IMobile
    ... this.right ...            -- IMobile

    METHODS:
    ... this.totalWeight() ...      -- int
    ... this.totalLength() ...      -- int
    ... this.isBalanced) ...        -- boolean
  
    METHODS FOR FIELDS:
    ... this.left.totalWeight() ...      -- int
    ... this.left.totalLength() ...      -- int
    ... this.left.isBalanced) ...        -- boolean

    ... this.right.totalWeight() ...      -- int
    ... this.right.totalLength() ...      -- int
    ... this.right.isBalanced() ...        -- boolean
*/

  // compute the total weight of this complex mobile
  int totalWeight(){
    return this.left.totalWeight() +
           this.right.totalWeight();
  }
  

}

/*
  Sample mobiles:
  ---------------
  
  simple1:
                     |
                     |
                     10
                    blue
  
  complex2 (with complex1 on the left and simple2 on the right)
                 complex1 (with simple3 on the left, simple1 on the right)
  
                         |
                         |
                         |
             ------------+------
             |                 |
       ------+------           |
       |           |           | 
       10          |           40    
      red          10         green
                  blue
*/
class Examples2 {
  Examples2(){}

  IMobile simple1 = new Simple(20, 10, new Blue());
  IMobile simple2 = new Simple(30, 40, new Green());
  IMobile simple3 = new Simple(10, 10, new Red());

  IMobile complex1 = new Complex(10, 5, 5, this.simple3, this.simple1);
  IMobile complex2 = new Complex(30, 10, 5, this.complex1, this.simple2);
  
  // test the method totalWeight for the IMobile class hierarchy
  boolean testTotalWeight =
    (check this.simple1.totalWeight() expect 10) &&
    (check this.complex1.totalWeight() expect 20) &&
    (check this.complex2.totalWeight() expect 60);
}



/*
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
  Pair Programming Assignment:
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
3.1 Problem
Start with the file soccer-team.java.
You are given the class definition and some sample data for classes that
represent information about a youth soccer league. The league keeps a list
of teams as follows. Each team is represented by its captain, the team name,
and a list of additional players on the team. The captain is also considered
to be a player. We also record the age of every player.
  
// CS U213 Spring 2009
// Assignment 3
// Problem 3.1
// October 2, 2008

/*   
            +-------------------------+
            |   +----------------+    |
            |   |                |    |
            v   v                |    |
         +---------+             |    |
         | ILoP    |             |    |
         +---------+             |    |
             / \                 |    |
             ---                 |    |
              |                  |    |
      ---------------------      |    |
      |                   |      |    |
  +-------+   +---------------+  |    |
  | MtLoP |   | ConsLoP       |  |    |
  +-------+   +---------------+  |    |
            +-| IPlayer first |  |    |
            | | ILoP rest     |--+    |
            | +---------------+       |
            v                         |
       +---------+                    |
       | IPlayer |                    |
       +---------+                    |
           / \                        |
           ---                        |
            |                         |
       ----------------------         |
       |                    |         |
  +-------------+   +--------------+  |
  | Member      |   | Captain      |  |
  +-------------+   +--------------+  |
  | String name |   | String name  |  |
  | int age     |   | int age      |  |
  +-------------+   | String team  |  |
                    | ILoP players |--+
                    +--------------+
*/

// to represent a list of players
interface ILoP {

}

// to represent an empty list of players
class MtLoP implements ILoP {
  MtLoP() {}

}

// to represent a list of players
class ConsLoP implements ILoP {
  IPlayer first; 
  ILoP rest;
  
  ConsLoP(IPlayer first, ILoP rest){
    this.first = first; 
    this.rest = rest; 
  } 

/* TEMPLATE:
   FIELDS:
    ... this.first ...             -- IPlayer
    ... this.rest ...              -- ILoP

    METHODS:
    ... this.mmm() ...            -- ???

    METHODS FOR FIELDS:
    ... this.first.nnn() ...             -- ??
    ... this.rest.mmm() ...              -- ???
*/
}

// to represent a player or a captain on a soccer team
interface IPlayer{}

// to represent a player on a soccer team
class Member implements IPlayer{
  String name;
  int age;
  
  Member(String name, int age){
    this.name = name;
    this.age = age;
  }
}

// to represent a captain on a soccer team
class Captain implements IPlayer{
  String name;
  int age;
  String team;
  ILoP players;
  
  Captain(String name, int age, String team, ILoP players){
    this.name = name;
    this.age = age;
    this.team = team;
    this.players = players;
  } 
}

// examples of soccer leagues data and tests for the methods
class Examples3{
  Examples3(){}
  
  ILoP mtlop = new MtLoP();
  
  // players -- team members of the team Real
  IPlayer tony = new Member("Tony", 19);
  IPlayer joey = new Member("Joey", 21);
  IPlayer pete = new Member("Pete", 18);
  IPlayer jake = new Member("Jake", 18);
  
  // list of player on the team Real
  ILoP realTeam = new ConsLoP(this.tony, 
                   new ConsLoP(this.joey, 
                    new ConsLoP(this.pete, 
                     new ConsLoP(this.jake, this.mtlop))));

  // players -- team members of the team Sting
  IPlayer jenny = new Member("Jenny", 19);
  IPlayer chris = new Member("Chris", 20);
  IPlayer kerry = new Member("Kerry", 22);
  IPlayer sasha = new Member("Sasha", 17);

  // list of player on the team Sting
  ILoP stingTeam = new ConsLoP(this.jenny, 
                    new ConsLoP(this.chris, 
                     new ConsLoP(this.kerry, 
                      new ConsLoP(this.sasha, this.mtlop))));
                     
  // players -- team members of the team Rex
  IPlayer kim = new Member("Kim", 19);
  IPlayer jan = new Member("Jan", 18);
  IPlayer cas = new Member("Cas", 21);
  IPlayer tim = new Member("Tim", 17);

  // list of player on the team Rex
  ILoP rexTeam = new ConsLoP(this.kim, 
                  new ConsLoP(this.jan, 
                   new ConsLoP(this.cas, 
                    new ConsLoP(this.tim, this.mtlop))));
  // captains
  IPlayer alex  = new Captain("Alex",  20, "Real",  this.realTeam);
  IPlayer ramon = new Captain("Ramon", 20, "Sting", this.stingTeam);
  IPlayer pat   = new Captain("Pat",   20, "Rex",   this.rexTeam);
  
  ILoP league = new ConsLoP(this.alex, 
                 new ConsLoP(this.ramon, 
                  new ConsLoP(this.pat, this.mtlop)));

}
/*
A. Write down on a paper the names of all teams, for each team list its
players, and the name of its captain. Submit this as a comment at the
end of your Examples class.
  
B. Design the method count that counts the total number of players in
this league. Design the templates as you go along. (We will grade the
templates!)

C. Design the method listAll that produces a list of all players in this
league.  
*/

/*
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
3.2 Problem
You are trying to organize the file directories on your computer. Your
friend gave you a start by designing the data definitions given in the 
filesdirectories.java file. She even gave you some examples of data.
  
// Assignment 3
// Problem 3.2
// CS U213 Spring 2009
// files-directories.java

/*
             +-----------------------+
             |  +-----------------+  |
             |  |                 |  |
             v  v                 |  |
         +-------+                |  |
         | ILoFD |                |  |
         +-------+                |  |
            / \                   |  | 
            ---                   |  |
             |                    |  |
      ---------------             |  |
      |             |             |  |
  +--------+  +------------+      |  |
  | MtLoFD |  | ConsLoFD   |      |  |
  +--------+  +------------+      |  |
            +-| IFD first  |      |  |
            | | ILoFD rest |------+  |
            | +------------+         |
            |                        |
            v                        |
           +-------+                 |
           | IFD   |                 |
           +-------+                 |
               / \                   |
               ---                   |
                |                    |
        -------------------          |
        |                 |          |
  +-------------+  +--------------+  |
  | File        |  | Directory    |  |
  +-------------+  +--------------+  |
  | String name |  | String name  |  |
  | int size    |  | ILoFD fdlist |--+
  | String kind |  +--------------+  
  +-------------+ 
*/


// to represent a list of files and directories in a computer file system
interface ILoFD{
  
}

// to represent an empty list of files and directories in a computer file system
class MtLoFD implements ILoFD{
  MtLoFD(){}  
}

// to represent a nonempty list of files and directories in a file system
class ConsLoFD implements ILoFD{
  IFD first;
  ILoFD rest;
  
  ConsLoFD(IFD first, ILoFD rest){
    this.first = first;
    this.rest = rest;
  }  

}

// to represent files and directories in a computer file system
interface IFD{
}


// to represent files in a computer file system
class File implements IFD{
  String name;
  int size;
  String kind;
  
  File(String name, int size, String kind){
    this.name = name;
    this.size = size;
    this.kind = kind;
  }
}

// to represent a directory in a computer file system
class Directory implements IFD{
  String name;
  ILoFD fdlist;
  
  Directory(String name, ILoFD fdlist){
    this.name = name;
    this.fdlist = fdlist;
  }

}
  
// Examples and tests for the class hierarchy for a computer file system
class Examples{
  Examples(){}
  
  /*
    Home:  
          Docs: 
                      text1
                      text2
          Pictures: 
                      pic1
                      Archives:   
                                  pic2
                                  pic3
  */
    
  IFD pic1 = new File("party", 6000, "jpeg");
  IFD pic2 = new File("beach", 3500, "jpeg");
  IFD pic3 = new File("river", 2900, "jpeg");
  
  IFD text1 = new File("letter", 600, "txt");
  IFD text2 = new File("memo", 500, "txt");
  
  ILoFD mtlofd = new MtLoFD();
  
  ILoFD archList = 
    new ConsLoFD(this.pic2, new ConsLoFD(this.pic3, this.mtlofd));    
  IFD archives = new Directory("Archives", this.archList);
  
  ILoFD picList = new ConsLoFD(this.pic1,
                    new ConsLoFD(this.archives, this.mtlofd)); 
  IFD pictures = new Directory("Pictures", this.picList);
                   
  ILoFD docList = new ConsLoFD(this.text1, 
                    new ConsLoFD(this.text2, this.mtlofd));                                        
  IFD docs = new Directory("Docs", this.docList);
  
  ILoFD homeList = new ConsLoFD(this.docs, 
                     new ConsLoFD(this.pictures, this.mtlofd));                                        
  IFD home = new Directory("Home", this.homeList);
}
  
/*
  A. Make an additional example(s) of data that allows us to represent the
following directory and its contents:
Directory Pages
contains the following: file index.html
file pictures.html
directory Pictures
directory Quotations
Directory Pictures
conatains the files: home.jpeg,
friend.jpeg.
brother.jpeg
Directory Quotations contains files: twain.txt,
cervantes.txt
Choose any sizes for these files. Assume the sizes are given in kilo-
Bytes.

  B. Design the method totalSize that computes the size of the directory
by adding the sizes of all files in it. Additionally, every directory
needs 4 kiloBytes for keeping track of its list of files and subdirectories.

  C. Design the method allFiles that produces a list of all files of the
given kind in the directory (and the names of all files in any of its
subdirectories, sub-subdirectories...).
Note: You must include the templates for all classes in this problem,
except the interfaces and classes that represent empty.
Note: Use helper method where appropriate.  
*/

/*
3.3 Problem: Extra Credit
Start with the file excel-cells.java.
For this problem you will use the classes that represent the values in the
cells of a spreadsheet. For each cell we record the row and column where
the cell is located, and the data stored in that cell. The data can either be a
numerical (integer) value or a formula. Each formula can be one of three
possible functions: + (representing addition), mn (producing the minimum
of the two cells), or * (computing the product) and involves two other cells
in the computation.
A. Make an example of the following spreadsheet segment:
| A | B | C | D | E |
---+----------+----------+----------+----------+-----------+
1 | 7 | 4 | 3 | 2 | 5 |
---+----------+----------+----------+----------+-----------+
2 | - A1 E1 | + B1 C1 | | | * A2 D1 |
---+----------+----------+----------+----------+-----------+
3 | | + B2 B1 | | | mn B3 D1 |
---+----------+----------+----------+----------+----------+
4 | | + B3 B2 | | | mn B4 D1 |
---+----------+----------+----------+----------+-----------+
5 | | + B4 B3 | | | * A2 E4 |
---+----------+----------+----------+----------+-----------+
B. Design the method value that computes the value of this cell.
C. Design the method countFun that computes the number of function
applications needed to compute the value of this cell.
D. Design the method countPlus that computes the number of Plus
applications needed to compute the value of this cell.
Make sure you design templates, use helper methods, and follow
the containment and inheritance arrows in the diagram.
*/

/*
3.4 Problem: Extra Credit
We are given two sorted files and would like to combine them into one file.
For simplicity we will deal with just files of Strings. The String class
defines the following method for comparing two Strings:
  
// compare this String to the given in lexicographical order
// produce an int < 0 if the this String is before the given
// produce 0 if this String is equal to the given
// produce an int > 0 if the this String is after the given
int compare(String that){ ... }
A. Design the method isSorted that determines whether this list of
Strings is sorted lexicographically.
B. Design the method merge that is invoked by a sorted list of Strings,
consumes another sorted list of Strings, and produces a new list of
Strings that contains all items from both lists in a sorted order.
Note: You must include the templates for all classes in this problem,
except the interfaces and classes that represent empty lists.
Note: Use helper method where appropriate.
  
*/

/*
3.5 Problem
Creative Project
Design the following game. Shooting stars are falling from the sky,
moving straight down at a constant speed on each tick. Each star has a
limited lifespan - after some number of ticks it burns out and disappears.
A geenie in a magical flying machine must collect stars as fuel for the flying
machine. The player steers the flying machine using the arrow keys (left,
right, up, down). When the flying machine catches a star, its fuel
supply increases and the star disappears. When the flying machine runs
out of fuel the game stops.
(Optionally, for extra credit, you may show the flying machine descending
to the ground and only then ending the game.)
A. Design the classes that represent one star, a list of stars, the flying
machine, and the whole game world.
B. Design the draw method for all classes that represent the components
of this game as well as for the class that represents your game world.
C. Design in the class Star the method onTick.
D. Design in the class Star the method makeStar that produces a new
star at a random horizontal position at the top of the Canvas with
some randomly chosen lifespan.
  
E. Design in the classes that represent a list of stars the method onTick
that produces a new list of stars by applying the onTick method to
each star in this list and replacing any stars that have burned out,
have been caught by the flying machine, or have hit the ground by a
new star produced by the makeStar method.
F. Design the method foundStar in the classes that represent a list of
stars that consumes an instance of the flying machine and determines
whether there is a star that the flying machine can catch. The flying
machine and the star must be close enough to each other.
G. Design the method caughtStar in the classes that represent a list of
stars that consumes an instance of the flying machine and produces a
star that the flying machine can catch. The flying machine and the star
must be close enough to each other. You must not invoke this method
unless you have verified that there is a star that can be caught. If two
or more stars can be caught, produce the first one you find.
H. Design the method catchStar in the class that represents the flying
machine that consumes a star and produces a flying machine with the
fuel supply increased by the star's remaining lifespan.
I. Design the method onKeyEvent in the class that represents the flying
machine that consumes a String that represents one of the arrow
keys and produces a star at the new location.
J. Design the method onKeyEvent in the class that represents your
game world. The method consumes a String that represents one of
the arrow keys and produces a new instance of the game world with
the flying machine moved as appropriate, checking whether any of
the stars can be caught, and ... - we will let you fill in the rest.
K. Finally, design the method onTick for the class that represents the
game world.
  
When you are ready to run the game do the following:
 Change the line that defines you GameWorld class to be:
class GameWorld extends World{
Of course, you will use whatever is the name of your class that defines
your world. If you have named it just World, you need to change its
name to something different.
 Change the language level to Intermediate ProfessorJ.
 Include on your Examples class the method
boolean go(){
return this.myInitialWorld.bigBang(200, 300, 0.1);
}
 assuming you have defined myInitialWorld in the Examples
class, want your Canvas to be 200 pixels wide and 300 pixels tall, and
want the clock tick at every 0.1 second. Of course, you choose your
own names, sizes, and the speed.
 Run the program, then in the Interactions window type the following:
> Examples e = new Examples();
> e.go()
 Have fun.

*/

